package com.seven.demo.java8;

import com.seven.demo.model.User;

import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

/**
 * 函数接口
 * @author Administrator
 *
 */
public class Test {
	
	public static void main(String[] args) {
		//函数接口Predicate<T> param:T return:boolean
		Predicate<Integer> predicate = p -> p > 8;
		System.out.println(predicate.test(2));
		//and
		Predicate<Integer> predicate2 = predicate.and(p2 -> p2 < 10);
		System.out.println(predicate2.test(9));
		//or
		Predicate<Integer> predicate3 = predicate.or(p3 -> p3 > 10);
		System.out.println(predicate3.test(9));
		//取反
		Predicate<Integer> predicate4 = predicate.negate();
		System.out.println(predicate4.test(8));
		
		
		//函数接口Comsumer<T>  param:T return:void
		Consumer<String> consumer = c -> System.out.println(c + "111");
		consumer.accept("hello");
		System.out.println();
		//andThen
		Consumer<String> consumer2 = consumer.andThen(c2 -> System.out.println(c2 + "222"));
		consumer2.accept("world");
		
		//函数接口Function<T,R>  param:T return:R
		Function<List<User>, List<String>> function = x -> x.stream().map(User::getName).collect(Collectors.toList());
		List<String> names = function.apply(Arrays.asList(new User(1,"aaa",23,"2018-8-8"),new User(1,"bbb",23,"2018-8-8")));
		System.out.println(names);
		
		//函数接口Supplier<T>  param:void return:T
		Supplier<User> supplier = () -> new User(1,"test",23,"2018-8-8");
		User user = supplier.get();
		System.out.println(user);
		
		//函数接口UnaryOperator<T> param:T return:T
		UnaryOperator<String> unaryOperator = x -> x;
		System.out.println(unaryOperator.apply("123"));
		
		//函数接口BinaryOperator<T> param:T,T return:T
		BinaryOperator<Integer> binaryOperator = (x,y) -> x + y;
		System.out.println(binaryOperator.apply(3, 5));
		
		//函数接口BiFunction<T,U,R> param:T,U return:R
		BiFunction<Integer, String, Boolean> biFunction = (x,y) -> (x + y).length() > 6;
		System.out.println(biFunction.apply(12, "test"));
	}

}



















